package fyp.mobile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import view.Core.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchDistrictActivity extends Activity {

	public static final int REQUEST_CODE = 1234;
	ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview01);

		ListView list = (ListView) findViewById(R.id.MyListView);

		SimpleAdapter mSchedule = new SimpleAdapter(this, mylist,
				R.layout.my_listitem2, new String[] { "ItemTitle", "ItemText",
						"img" }, new int[] { R.id.ItemTitle, R.id.ItemText,
						R.id.img });

		list.setAdapter(mSchedule);

		list.setOnItemClickListener(new OnItemClickListener() {

			HashMap<String, Object> item = new HashMap<String, Object>();

			@Override
			public void onItemClick(android.widget.AdapterView<?> arg0,
					View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				onItemSelected(null, arg1, arg2, arg3);

			}
		});
		listAddItem();
	}

	public void listAddItem() {
		HashMap<String, Object> item = new HashMap<String, Object>();
		item.put("ItemTitle", "Central and Western District");
		item.put("ItemText", "D0001");
		item.put("img", R.drawable.a1);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Wan Chai District");
		item.put("ItemText", "D0002");
		item.put("img", R.drawable.a15);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Eastern District");
		item.put("ItemText", "D0003");
		item.put("img", R.drawable.a2);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Southern District");
		item.put("ItemText", "D0004");
		item.put("img", R.drawable.a11);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Sham Shui Po District");
		item.put("ItemText", "D0005");
		item.put("img", R.drawable.a9);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Yau Tsim Mong District");
		item.put("ItemText", "D0006");
		item.put("img", R.drawable.a17);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Kowloon City District");
		item.put("ItemText", "D0007");
		item.put("img", R.drawable.a4);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Wong Tai Sin District");
		item.put("ItemText", "D0008");
		item.put("img", R.drawable.a16);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Kwun Tong District");
		item.put("ItemText", "D0009");
		item.put("img", R.drawable.a6);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Islands District");
		item.put("ItemText", "D0010");
		item.put("img", R.drawable.a3);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Tuen Mun District");
		item.put("ItemText", "D0011");
		item.put("img", R.drawable.a14);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Yuen Long District");
		item.put("ItemText", "D0012");
		item.put("img", R.drawable.a18);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Kwai Tsing District");
		item.put("ItemText", "D0013");
		item.put("img", R.drawable.a5);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Tsuen Wan District");
		item.put("ItemText", "D0014");
		item.put("img", R.drawable.a13);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "North District");
		item.put("ItemText", "D0015");
		item.put("img", R.drawable.a17);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Tai Po District");
		item.put("ItemText", "D0016");
		item.put("img", R.drawable.a12);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Sha Tin District");
		item.put("ItemText", "D0017");
		item.put("img", R.drawable.a10);
		mylist.add(item);

		item = new HashMap<String, Object>();
		item.put("ItemTitle", "Sai Kung District");
		item.put("ItemText", "D0018");
		item.put("img", R.drawable.a8);
		mylist.add(item);

	}

	public void errorToast(String msg) {
		Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);

		menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "Back").setShortcut('0',
				'a');
		menu.add(Menu.NONE, Menu.FIRST + 2, Menu.NONE, "Help").setShortcut('1',
				'b');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			finish();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "Your Choice: " + title, Toast.LENGTH_LONG)
					.show();
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void show_alert() {

		AlertDialog.Builder alert_box = new AlertDialog.Builder(this);
		alert_box.setMessage("Do you want to quit the app?");
		alert_box.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						onDestroy();
					}
				});

		alert_box.setNegativeButton("No",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		alert_box.show();
	}

	/*
	public void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
	*/

	public void onItemSelected(AdapterView arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		HashMap<String, Object> item = new HashMap<String, Object>();
		item = mylist.get(position);
		String districtID = item.get("ItemText").toString();
		Intent intent = new Intent();
		intent.setClass(this, DistrictTreeActivity.class);
		intent.putExtra("districtID", districtID);
		startActivityForResult(intent, REQUEST_CODE);
	}

	public void onNothingSelected(AdapterView arg0) {
		// TODO Auto-generated method stub

	}

}
